/* Using FSRead to Read from a File
* prompts the user for a text file and then writes the contents of that file to the
* console window. It is intended only as an example and so contains a minimum of
* error handling.
*/
// Assumes inclusion of
#include
#include
void ToolBoxInit (void);
void GetAFile (Handle* handleToFileData, long *fileSize); void ToolBoxInit ()
{
}
void GetAFile (Handle* handleToFileData, long *fileSize) {
short fileRefNum;
long count;
typeList[0] = 'TEXT';
SFGetFile (where, nil, nil, 1, typeList, nil, &aReply); if (aReply.good)
{
if (!err) {
err = GetEOF (fileRefNum, fileSize); if (!err) {
if (fileSize == 0)
{
printf ("Nothing in file\n");
exit (1);
}
if (* handleToFileData == nil)
{
printf ("Sorry, the file is too big to read at once\n");
exit (1);
}
HLock (* handleToFileData); count = *fileSize;
err = FSRead (fileRefNum, &count, ** handleToFileData); if ((err) || (count != *fileSize)) {
printf ("Problem reading file\n");
exit (1);
}
}
else {
printf ("Problem getting size of file\n");
exit (1);
}
}
else {
printf ("Problem opening file\n");
exit (1);
}
}
}
main ()
{
short i;
long fileSize;
ToolBoxInit();
GetAFile (& handleToData, &fileSize);
for (i = 0; i < fileSize; i++)
if (((* handleToData)[i]) == '\r')
putchar ('\n');
else
putchar ((* handleToData)[i]);
}